package com.gaoyi.crm.repository;

import com.gaoyi.crm.apiresult.ApiResultData;
import com.gaoyi.crm.callback.OnResultListener;
import com.gaoyi.crm.constant.ApiStatus;
import com.gaoyi.crm.constant.DbMsg;
import com.gaoyi.crm.db.dao.PmnItemNoDao;
import com.gaoyi.crm.db.entity.ItemNoStore;
import com.gaoyi.crm.db.entity.PmnItemNo;
import com.gaoyi.crm.repository.base.BaseRepository;
import com.gaoyi.crm.util.OKHttpUtil;
import com.gaoyi.crm.util.ThreadManager;
import com.gaoyi.crm.util.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class PmnItemNoRepository extends BaseRepository<PmnItemNo, PmnItemNoDao> {
    @Override
    protected int getIndex() {
        return 0;
    }

    @Override
    protected PmnItemNoDao getDao() {
        return mDatabase.getPmnItemNoDao();
    }

    public void insertPmnItemNo(List<PmnItemNo> pmnItemNos) {
        mDao.inserts(pmnItemNos);
    }

    /**
     * 根据id查询Pmn
     */
    public void getPmnItemNo(String id, OnResultListener onResultListener) {
        ThreadManager.getInstance().execute(() -> {
            String name = Utils.getName();
            Map<ItemNoStore,String> maps = mDao.getItemNoByPmnId(mDao.getItemNoByPmnIdSql(name, id));
            Set<ItemNoStore> set = maps.keySet();
            List<ItemNoStore> itemNoStores = new ArrayList<>();
            for (ItemNoStore itemNoStore : set) {
                String categoryName = maps.get(itemNoStore);
                itemNoStore.setCategoryName(categoryName);
                itemNoStores.add(itemNoStore);
            }
            ApiResultData<List<ItemNoStore>> result = new ApiResultData<>();
            result.setStatus(ApiStatus.SUCCESS);
            result.setMessage(DbMsg.QUERY_SUCCESS);
            result.setData(itemNoStores);
            OKHttpUtil.getInstance().post(() -> onResultListener.onSuccess(result));
        });
    }

    public void deleteByPmnId(String id){
        mDao.deleteByPmnId(id);
    }
}
